{include file="public/layout" /}
<body class="laytp-container">
<div class="layui-card" id="search-form">
    <div class="layui-card-body">
        <form class="layui-form layui-row layui-col-space16 layui-search-admin">
            <div class="layui-inline">
                <label class="layui-form-label">关键词</label>
                <div class="layui-input-inline" >
                    <input type="text" name="keyword" placeholder="请输入关键词" lay-affix="clear" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label"></label>
                <button class="layui-btn" lay-submit lay-filter="demo-table-search">搜索</button>
                <button type="reset" class="layui-btn layui-btn-primary">清空</button>
            </div>
        </form>
    </div>
</div>
<div class="layui-card">
    <div class="layui-card-body">
        <div class="layui-btn-container">
            <button class="layui-btn layui-btn-sm" onclick="add()">添加</button>
            <button class="layui-btn layui-btn-sm" onclick="datadel(0)">删除</button>
        </div>
        <table id="tabledata" lay-filter="tabledata"></table>
    </div>
</div>
<script>
var table = layui.table;
var datawhere = {};
//数据表
var tableIns = table.render({
    elem: '#tabledata',
    url: "{$Request.url}", //数据接口
    toolbar: '#toolbarDemo',
    defaultToolbar: ['filter', 'exports', 'print', {
        title: '提示',
        layEvent: 'LAYTABLE_TIPS',
        icon: 'layui-icon-tips'
    }],
    height: 'full-35', // 最大高度减去其他容器已占有的高度差
    css: [ // 重设当前表格样式
        '.layui-table-tool-temp{padding-right: 145px;}'
    ].join(''),
    cellMinWidth: 80,
    totalRow: false, // 开启合计行
    page: true,
    pagebar: '#test-pagebar', // 分页栏模板,
    cols: [[ //表头
        {type:"checkbox", fixed: 'left'},
        {field: 'id', title: 'ID', width:80, sort: true},
        {field: 'name', title: '名称'},
        {field: 'keyword', title: '关键词'},
        {field: 'sort', title: '序号', width:80, sort: true},
        {field: 'status', title: '状态', templet:function(d){ return d.status==1?'<span style="color:green">显示</span>':'<span style="color:red">隐藏</span>'}, width:80},
        {fixed: 'right', title:'操作', width: 134, minWidth: 125, templet: function(d){
            var html = '';
            html += '<a class="layui-btn layui-btn-xs" onclick="add('+d.id+')">编辑</a>';
            html += '<a class="layui-btn layui-btn-xs" onclick="datadel('+d.id+')">删除</a>';
            return html;
        }}
    ]]
});
	//排序
	table.on('sort(tabledata)', function(obj){
		datawhere.field = obj.field;
		datawhere.order = obj.type;
		tableIns.reload({
			initSort: obj,
			where: datawhere
		});
	});
	//检索
	layui.form.on('submit(LAY-app-forumreply-search)', function(obj){
		var field = obj.field
		var olddatawhere = datawhere
		datawhere = field
		datawhere.field = olddatawhere.field
		datawhere.order = olddatawhere.order
		tableIns.reload({
			where: datawhere,
			page: {curr: 1}
		});
	})
	//删除
	function datadel(id){
		var ids = [];
		if(id==0){
			var checkStatus = table.checkStatus('tabledata')
			var checkData = checkStatus.data; //得到选中的数据
			if(checkData.length === 0){
				 return layer.msg('请选择数据');
			}
			var ids = [];
			for(var i=0;i<checkData.length;i++){
				ids.push(checkData[i]['id']);
			}
		}else{
			ids.push(id)
		}
		layer.confirm('确定要删除吗?',{icon: 7, title:'操作确认'}, function(index){
			//do something
			layer.close(index);
			var index = layer.load();
			$.post("{:url('del')}",{ids:ids},function(data){
				layer.close(index);
				dialog(data.msg,data.status);
				tableIns.reload()
			})
		});
	}
  function add(id){
	  layer.open({
		  title: '添加',
		  type: 2,
		  shadeClose: true,
		  area: ['100%','100%'],
		  scrollbar:false,
		  content: '{:url("edit")}/id'+id
	  });
  }
	</script>
	<div id="NewsToolBox"></div>
</body>
</html>